package top.igotcha.mapreduce.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @time 2021/1/11
 * @author Gotcha
 * @describe
 */
public class SortWritable implements WritableComparable<SortWritable> {
    private String first;
    private int second;

    //实现排序规则
    @Override
    public int compareTo(SortWritable other) {
        //先比较first,相同则比较second
        int res = this.first.compareTo(other.getFirst());
        if (res!=0){
            return res;
        } else {
            return this.second - other.getSecond();
        }
    }

    //实现序列化
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(first);
        dataOutput.writeInt(second);
    }

    //实现反序列化
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.first = dataInput.readUTF();
        this.second = dataInput.readInt();
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    @Override
    public String toString() {
        return first + '\t' + second ;
    }
}
